--region NewFile_1.lua
--Author : zhoujibo
--Date   : 2015/6/6
--此文件由[BabeLua]插件自动生成




function _ps(arg0)
    local currentKeys = nil
    local currentTable = nil
    local currentIndex = nil
    local stack = {} --{table,keys,index}
    local tableChain = {}
    local lines = {}
    local function add(key,s,isLast)
        if key~=nil then
            if type(key)== 'string' then
                key = "'"..key.."'"
            end
            s = tostring(key)..':'..tostring(s)
        else
            s = tostring(s)
        end
        local indent = #stack+(currentTable and 1 or 0)
        local line = ''
        if indent>=1 then --└├─│
            line = isLast and '└─' or '├─'
            indent = indent - 1
        end
        if indent > 0 then
            for i=1,indent do
                _,_,_,_isLast = unpack(stack[i])
                if _isLast then
                    line = '    '..line
                else
                    line = '│  '..line
                end
            end
        end
        line = line..s
        table.insert(lines,line)
    end
    local function isInteger(a)
        return math.floor(a)==a
    end
    local function getType(a)
        local t = type(a)
        if t=='number' and isInteger(a)then
            t = 'int'
        end
        return t
    end
    local function isSingleValue(a)
        local t = type(a)
        if t=='table' then
            for _,_ in pairs(a)do
                return false
            end
        end
        return true
    end
    local function getSortedKeys(tObj)
        local arr = table.dicKeys(tObj)
        table.sort(arr,function(a,b)
            local aType = getType(a)
            local bType = getType(b)
            if aType == 'int' and bType=='int' then
                if(a>0 and b>0)then
                    return a<b
                elseif(a<0 and b<0)then
                    return a>b
                elseif(a==0)then
                    return b<0
                elseif(b==0)then
                    return a>0
                else
                    return a>0
                end
            elseif aType~='int' and bType~='int' then
                return tostring(a)<tostring(b)
            else
                return aType=='int'
            end
        end)
        return arr
    end
    local function getTableAddress(t)
        local addr = string.sub(tostring(t),string.len('table: '))
        addr = string.trim(addr)
        if string.sub(addr,1,2)~='0x' then
            addr = '0x'..addr
        end
        return addr
    end
    while true do
        local arg0Key = nil
        local tablePopup = false
        local isLast = false
        if currentKeys then
            arg0Key = currentKeys[currentIndex]
            isLast = currentIndex==#currentKeys
            currentIndex = currentIndex+1
            if arg0Key == nil then
                if #stack > 0 then
                    table.remove(tableChain)
                    currentTable,currentKeys,currentIndex,isLast = unpack(table.remove(stack))
                    tablePopup = true
                else
                    break
                end
            else
                arg0 = currentTable[arg0Key]
            end
        end
        if not tablePopup then
            local t = type(arg0)
            if isSingleValue(arg0)then
                if t == 'string' then arg0 = "'"..arg0.."'" end
                if t == 'table' then arg0 = '{}' end
                add(arg0Key,arg0,isLast)
                if #stack==0 and arg0Key == nil then
                    break
                end
            else
                local keys = getSortedKeys(arg0)
                local address = getTableAddress(arg0)
                local i = 1
                local isArray = true
                for k in pairs(arg0)do
                    if i ~= k then
                        isArray = false
                        break
                    end
                    i = i+1
                end
                
                add(arg0Key,(isArray and '[array' or '[dict')..' '..address..']:',isLast)
                
                if currentKeys then
                    table.insert(tableChain,currentTable)
                    table.insert(stack,{currentTable,currentKeys,currentIndex,isLast})
                end
                if table.indexOf(tableChain,arg0) then
                    add(nil,'[CYCLE STRCUTURE]...',true)
                    table.remove(stack)
                else
                    currentKeys = keys
                    currentTable = arg0
                    currentIndex = 1
                end
            end
        end

    end
    return table.concat(lines,'\n')
end

function ps(...)
    local arr = {}
    for i=1,select('#',...)do
        table.insert(arr,_ps(select(i,...)))
    end
    return table.concat(arr,'\n===========\n')
end

function pp(...)
    print('\n'..ps(...))
end


--endregion
